import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import QtQml.Models 2.13
import QtQuick.Controls.Styles 1.4
import et.dirsearch 1.0
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2

Item {
    id:root
    visible: true
    anchors.fill: parent
    //width: 800
    //height: 500
    //title: qsTr("CT报表 V1.0")

    ColumnLayout{
        spacing: 2

        RowLayout{
            id:firstrow
            Layout.preferredHeight: 30
            Layout.margins: 1
            spacing: 2
            Label{
                text: "CL2030"
            }

            //            Label{
            //                text: "编号"
            //            }

            //            Rectangle {
            //                color: "white"
            //                border.color: "gray"
            //                Layout.fillWidth: true
            //                Layout.minimumWidth: 200
            //                Layout.preferredWidth: 200
            //                Layout.maximumWidth: 300
            //                Layout.minimumHeight: 25

            //                TextInput {
            //                    id: conditiona
            //                    text: qsTr("")
            //                    width: parent.width
            //                    font.pointSize: 18
            //                    selectByMouse: true
            //                    x:2
            //                }
            //            }

            //            Label{
            //                text: "检定时间"
            //            }
            //            Rectangle {
            //                color: "white"
            //                border.color: "gray"
            //                Layout.fillWidth: true
            //                Layout.minimumWidth: 100
            //                Layout.preferredWidth: 100
            //                Layout.maximumWidth: 100
            //                Layout.minimumHeight: 25

            //                TextInput {
            //                    id: conditiona2
            //                    text: Qt.formatDateTime(new Date(), "yyyy-MM-dd");
            //                    width: parent.width-2
            //                    font.pointSize: 12
            //                    selectByMouse: true
            //                    x:2
            //                    anchors.centerIn: parent;
            //                }
            //            }
            //            //            Calendar {
            //            //                  minimumDate: new Date(2017, 0, 1)
            //            //                  maximumDate: new Date(2018, 0, 1)
            //            //                  frameVisible: true

            //            //              }

            //            Button{
            //                text: qsTr("查找");
            //                Layout.preferredHeight: 25;
            //                Layout.preferredWidth: 80;
            //                //style:btnStyle;//Controls 1.x

            //            }
            Button{
                text: qsTr("刷新");
                Layout.preferredHeight: 25;
                Layout.preferredWidth: 80;
                onClicked: {
                    runBusyDialog();
                    console.log("refresh log");
                    dirsearch.searchFiles();
                    tableView.records=null;
                    tableView.records=dirsearch;
                    endBusyDialog();
                }
            }
            Button{
                text: qsTr("导出xlsx");
                Layout.preferredHeight: 25;
                Layout.preferredWidth: 100;
                onClicked: {
                    runBusyDialog();
                    console.log("records log");
                    dirsearch.exportExcelZh();
                }
            }
            Button{
                text: qsTr("Export xlsx");
                Layout.preferredHeight: 25;
                Layout.preferredWidth: 100;
                onClicked: {
                    runBusyDialog();
                    console.log("records log en");
                    dirsearch.exportExcelEn();
                }
            }
            Button{
                text:qsTr("删除");
                Layout.preferredHeight: 25;
                Layout.preferredWidth: 100;
                onClicked: {
                    msgboxDelete.open();
                }
            }
        }

        ResultsTableView{
            id:tableView
            Layout.fillWidth: true;
            Layout.fillHeight: true;
            Layout.preferredWidth: root.width;
            Layout.preferredHeight: root.height-firstrow.height;

        }

    }

    Component.onCompleted: {
        dirsearch.searchFiles();
        tableView.records=dirsearch;
    }

    Component{
        id:btnStyle;
        ButtonStyle{
            background: Rectangle{
                implicitWidth: 120;
                implicitHeight: 25;
                color: "#DDDDDD";
                border.width: control.hovered ? 2 : 1;
                border.color: control.hovered ? "green" : "#888888";
            }
        }
    }
    Connections{
        target: dirsearch            //在外部连接MouseArea的点击信号
        onExcelFinished:{
            showMsgBox();
            console.log("onexcelFinished");
        }
        onExcelIdle:{
            endBusyDialog();
        }
    }

    BusyIndicator{
        id:busyDialog
        visible: false
        anchors.centerIn: parent
        running: false
    }

    Dirsearch{
        id:dirsearch
    }

    function runBusyDialog(){
        busyDialog.running=true;
        busyDialog.visible=true;
    }
    function endBusyDialog(){
        busyDialog.running=false;
        busyDialog.visible=false;
    }
    function showMsgBox(){
        msgbox.open();
    }

    MessageDialog{
        id:msgbox
        icon: StandardIcon.Information;
        standardButtons: StandardButton.Ok;
        title: qsTr("提示");
        text: qsTr("完成");
    }
    MessageDialog{
        id:msgboxDelete
        icon: StandardIcon.Information;
        standardButtons: StandardButton.Yes | StandardButton.No
        modality: Qt.ApplicationModal;
        title: qsTr("提示");
        text: qsTr("确认删除选中记录");
        onYes: {
            runBusyDialog();
            console.log("records log delete");
            dirsearch.deleteRecords();
        }
    }
}
